package cn.net.isir.core.datasource;

import org.aspectj.lang.JoinPoint;

/**
 * @author liuxubo
 * @filename DataSourceExchange.java
 * @package cn.net.isir.core.datasource
 * @email lxb@isir.net.cn
 * @date 2019/02/21
 * @comment
 * @since 1.8
 */
public class DataSourceExchange {

    public void before(JoinPoint point) {

        //获取目标对象的类类型
        Class<?> aClass = point.getTarget().getClass();
        String c = aClass.getName();
        String[] ss = c.split("\\.");
        //获取包名用于区分不同数据源
        String packageName = ss[3];
        String packageName2 = ss[4];
        //cn.net.isir.service

        //放在不同service文件夹，或者调用不同的父类
        if ("service".equals(packageName) || ("core".equals(packageName) && "BasicService".equals(packageName2))) {
            DataSourceHolder.setDataSources("ds1");
            System.out.println("数据源：ds1");
        } else {
            DataSourceHolder.setDataSources("ds2");
            System.out.println("数据源：ds2");
        }
    }

    /**
     * 执行后将数据源置为空
     */
    public void after() {
        DataSourceHolder.setDataSources(null);
    }
}
